const qs = require('./qs/lib/index')
App({

    onLaunch: function () {
        wx.setStorageSync('user_id', "20171120122441321906");
        wx.setStorageSync('token_1', "Bearer eyJhbGciOiJIUzUxMiJ9.eyJleHAiOjE1MTY3ODM3OTksInN1YiI6IuWkqeaxoOiDvea6kCIsImNyZWF0ZWQiOjE1MTY3ODE5OTk2Nzh9.2wgyxgj8FJ-p5UMwfzLHKbgGX3azKoosasS9cTq3ecu5fduzfxDM4K7-r1pgSdT804jfGRgTJt99ICLm_ND5-Q");
        if (wx.getStorageSync('login_time') == "") {
            wx.setStorageSync("login_time", 1400000000);
        }
        // var time = new Date(new Date(new Date().toLocaleDateString()).getTime())/1000;
        // if (wx.getStorageSync('login_time') < time) {
        //   wx.setStorageSync("login_time", time);
        //   wx.showModal({
        //     title: '提醒',
        //     content: '当前绑定车型：'+wx.getStorageSync('carType')+'\r 当前绑定车号：'+wx.getStorageSync('carNo')+'\r 是否修改绑定?',
        //     success: function(res) {
        //       if (res.confirm) {
        //         wx.navigateTo({
        //           url: '/pages/mine/changecar'
        //         })
        //       }
        //     }
        //   })
        // }
    },
    onShow: function (e) {
        if (e.shareTicket) {
            console.log(e.shareTicket);
        }
        this.getToken();
        setTimeout(function () {
            getApp().checknewmsg();
        }, 5000);
    },
    checkLogin: function () {
        var user_id = wx.getStorageSync('user_id');

        if (user_id == undefined || user_id == null || user_id == "") {
            wx.showModal({
                title: '提示',
                content: '您需要先登录！',
                showCancel: false,
                success: function (res) {
                    wx.redirectTo({
                        url: '/pages/login/login'
                    })
                }
            })
        }
    },
    onHide: function () {

    },
    addListenerchatlist: function (chatlistcallback) {
        this.chatlistcallback = chatlistcallback;
    },
    addListenerindex: function (indexcallback) {
        this.indexcallback = indexcallback;
    },
    addListenerchat: function (chatcallback) {
        this.chatcallback = chatcallback;
    },
    setChangedData: function (data) {
        this.data = data;
        if (this.indexcallback != null) {
            this.indexcallback(data);
        }
        // if (this.chatlistcallback != null) {
        //   this.chatlistcallback(data);
        // }
        if (this.chatcallback != null) {
            this.chatcallback(data);
        }
    },
    alert: function (content) {
        wx.showModal({
            title: '提示',
            content: content
        })

    },
    toast: function (content) {
        wx.showToast({
            title: content,
            mask: true,
            icon: 'success',
            duration: 1000
        })
    },
    sp: function (content = "加载中") {
        wx.showLoading({
            title: content,
            mask: true
        })
    },
    hp: function () {
        wx.hideLoading();
    },
    checknewmsg: function () {
        var that = this;
        var user_id = wx.getStorageSync('user_id');
        if (user_id != undefined && user_id != null && user_id != "") {
            wx.request({
                url: getApp().globalData.javaurl + '/common/chat/getNewMsg',
                method: 'POST',
                header: {
                    "content-type": "application/x-www-form-urlencoded",
                    "Authorization": wx.getStorageSync('token')
                },
                success: function (res) {
                    if (res.header.Authorization != "" && res.header.Authorization != undefined) {
                        wx.setStorageSync('token', res.header.Authorization);
                    }
                    if (res.header.authorization != "" && res.header.authorization != undefined) {
                        wx.setStorageSync('token', res.header.authorization);
                    }
                    that.setChangedData(res.data.data);
                    setTimeout(function () {
                        getApp().checknewmsg();
                    }, 5000);
                }
            });
        }

    },
    getToken: function () {
        var that = this;
        wx.request({
            url: that.globalData.javaurl + '/login',
            method: 'POST',
            dataType: "json",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                "username": wx.getStorageSync('login_name'),
                "password": wx.getStorageSync('pwd')
            },
            success: function (res) {
                console.log(res.data);
                wx.setStorageSync('token', res.data.data.token);
            }
        });
    },
    /**
     * TODO(封装带Token的请求)
     * @param
     * @auther zhangQ
     * @date 2018/1/24 13:20
     **/
    requestToken: function (param) {
        if (!(param && param.url)) {
            return '参数错误！'
        }
        let headerObj = param.header ? param.header : {}
        headerObj['content-type'] = 'application/x-www-form-urlencoded'
        let sendData = {}
        if (param.data) {
            sendData = param.data
        }
        let Authorization = wx.getStorageSync('token_1')
        if (Authorization) {
            sendData.authorization = Authorization
        } else {
            return '参数错误！'
        }
        // 转码
        debugger
        sendData = qs.stringify(sendData)
        return wx.request({
            url: param.url,
            method: param.method ? param.method : 'POST',
            dataType: param.dataType ? param.dataType : 'json',
            header: headerObj,
            data: sendData,
            success: function (res) {
                if (param.success && typeof param.success == "function") {
                    param.success(res)
                }
            },
            fail: function () {
                if (param.fail && typeof param.fail == "function") {
                    param.fail()
                }
            },
            complete: function () {
                if (param.complete && typeof param.complete == "function") {
                    param.complete()
                }
            }
        })
    },
    /**
     * TODO(封装不带Token的promise请求)
     * @param
     * @auther zhangQ
     * @date 2018/1/24 18:13
     **/
    request: function (param) {
    },
    globalData: {
        apiurl: 'https://tp.x-link.cc/DriverApi/',
        // javaurl: 'https://java.x-link.cc',
        javaurl: 'http://localhost:8080',
        weidu: 0
    }
});